let readlineSync = require('readline-sync');

function getUserInput() {
    return parseInt(readlineSync.question(`1. add student information
2. produce a transcript
3. exit
Please enter your choice (1~3):`));
}

function oneTest(input){
    let str = /([\u4e00-\u9fa5]{1,4}),([0-9]{10}),([\u4e00-\u9fa5]),((\d){1,10}),([\u4e00-\u9fa5]{1,10}):([0-9]{1,3}),([\u4e00-\u9fa5]{1,10}):([0-9]{1,3}),([\u4e00-\u9fa5]{1,10}):([0-9]{1,3}),([\u4e00-\u9fa5]{1,10}):([0-9]{1,3})/;
    if(str.test(input)){
        let information=input.split(',');
        let student = {
            name:information[0],
            studentId:information[1],
            nation:information[2],
            class:information[3]
        }
        let studentGrade=information[4];
        for(let i in studentGrade){
            let subject = studentGrade[i].split(/:/);
            studentMainInfo[subject[0]] = Number(subject[1]);
        }
       
        console.log(`学生${student.name}的成绩被添加\n`);
        return student;
    }
    if(input === '0'){
        return true;
    }
    else{
        input=readlineSync.question(`Please input in the correct format (Format: name, school number, discipline: grades,...):\n`)
        oneTest(input);
    }
}

function one(){
    let input = readlineSync.question(`Please input student information (Format: name, student number, nationality, class, grade: subject:...), press enter to submit:\n`);
    
    return oneTest(input);
}
function compute_median(a) {
    a.sort((x,y)=>x-y);
    if(a.length %2 === 0){
        return  ((a[a.length/2]+a[a.length/2-1])/2);
    }
    else{
        return  (a[parseInt(a.length/2)]);
    }
}
function twoTest(input,student){
    let patt = /((\d){10},)*/;
    if(patt.test(input)){
        let studentId = input.splice(',');
        let allScore = [];
        for(let item of students){
            let personalTotalScore = item["数学"]+item["语文"]+item["英语"]+item["编程"];
            item.sum = personalTotalScore;
            allScore.push(personalTotalScore);
        }
        let result = "成绩单\n" +"姓名|数学|语文|英语|编程|平均分|总分 \n";
        result += "========================\n";
        for(let item1 of studentId){
            for(let item2 of student){
                if(item.studentId === item1){
                    result += item.name+item["数学"]+'|'+item["语文"]+'|'+item["英语"]+'|'+item["编程"]+'|';
                    result += item.sum/4 + '|' + item.sum + '\n'
                }
            }
        }
        result += "========================\n";
        if(allScore.length === 0){
            return 1;
        }
        else{
            result += "全班总分平均数：" + allScore.reduce((x,y)=>x+y)/allScore.length + '\n';
            result += "全班总分中位数：" + compute_median(allScore);
        }
        return result;
    }
    if(input === '0'){
        return true;
    }
    else{
        input = readlineSync.question(`请按正确的格式输入要打印的学生的学号（格式： 学号, 学号,...），按回车提交：`);
        twoTest(input,student);
    }
}

function two(student){
    
    let input = readlineSync.question(`Please enter the student ID to print (Format: Student ID, student ID,...), press enter to submit:`);
    
    twoTest(input,student);
}
function three(){
    console.log('Have withdrawn');
}

function main(){
    let students=[];
    switch(getUserInput()){
        case 1:
        students.push(one());
        main();
        break;
        case 2:
        two(students);
        main();
        break;
        case 3:
        three();
    }
}
main();